package org.ptkparty.shared.model;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.datanucleus.api.jpa.annotations.Extension;
import org.ptkparty.server.dao.EMFService;

import com.google.appengine.api.users.UserServiceFactory;

@Entity
public class PlaceEntity {

	private static final Logger LOGGER = Logger.getLogger(PlaceEntity.class.getName());

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String id;

	@Version
	private Integer version;

	@NotNull
	private String name;

	private Double latitude;

	private Double longitude;

	private String creator;

	public static PlaceEntity findPlaceEntity(String id) {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			return em.find(PlaceEntity.class, id);
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("unchecked")
	public static List<PlaceEntity> getAll() {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			List<PlaceEntity> places = em.createQuery("SELECT p from PlaceEntity p").getResultList();
			places.size();
			return places;
		} catch (Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
		} finally {
			em.close();
		}
		return null;
	}

	public static void save(List<PlaceEntity> championships) {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			String userId = UserEntity.getUser(UserServiceFactory.getUserService().getCurrentUser().getEmail()).getId();
			for (PlaceEntity place : championships) {
				if (place.getCreator() == null) {
					place.setCreator(userId);
				}
				if (place.getId() == null) {
					em.persist(place);
				} else {
					em.merge(place);
				}
			}
		} catch (Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
		} finally {
			em.close();
		}
	}

	public void remove() {
		if (this.getId() != null) {
			EntityManager em = EMFService.get().createEntityManager();
			try {
				em.remove(this);
			} catch (Exception e) {
				LOGGER.log(Level.SEVERE, e.getMessage(), e);
			} finally {
				em.close();
			}
		}
	}

	private Object getCreator() {
		return creator;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Double getLatitude() {
		return latitude;
	}

	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}

	public Double getLongitude() {
		return longitude;
	}

	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}
}
